import importData from "./importData";
import {isEmpty} from "../../../js/commonUtils";

export const initPageInfo = () => {
  //查询参数
  importData.tParam.searchParam.from = null;
  importData.tParam.searchParam.tag = null;
  importData.tParam.searchParam.state = 3;
  importData.tParam.searchParam.shopId = "";
  importData.tParam.searchParam.pCode = "";
  importData.tParam.searchParam.categories.splice(0);
  importData.tParam.searchParam.auditStartTime = null;
  importData.tParam.searchParam.auditEndTime = null;
  importData.tParam.searchParam.importState = -1;
  importData.tParam.searchParam.stationBanned = null;
  importData.tParam.searchParam.isAuditable = true;
  importData.tParam.searchParam.author = "";

  //分页
  importData.tPage.listPage.pageNum = 1;
  importData.tPage.listPage.pageSize = 40;
  importData.tPage.listPage.total = 0;

  //数据
  importData.tData.list.splice(0);
}

export const queryProductList = (self) => {
  let params = importData.tParam.searchParam;
  params.pageNum = importData.tPage.listPage.pageNum;
  params.pageSize = importData.tPage.listPage.pageSize;
  self.$Spin.show();
  self.$apiImpProduct.queryAuditedProductList(params).then(response => {
    self.$Spin.hide();
    importData.tData.list.splice(0);
    if(response.success) {
      let page = response.result;
      importData.tData.list.push(...page.list);
      importData.tPage.listPage.total = page.total;
    }else{
      self.$Message.error(response.errorMessage);
    }
  });
}

export const importToFormalLib = (self) => {
  let params = importData.tParam.importParam;
  self.$Spin.show();
  self.$apiImpProduct.putInQueue(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      //关闭弹窗
      importData.tStatus.profitRatioModal.isShow = false;
      //清空已选列表
      self.resetSelectedProducts();
      //刷新列表
      queryProductList(self);
      //提示导入中
      self.$Message.info({content: "已加入导入队列，导入过程可点击导入记录查看", duration: 5});
      //开始导入
      // params.createtime = response.result;
      // self.$apiImpProduct.importProduct(params);
    }else{
      console.error(response.errorMessage);
      self.$Message.error("加入导入队列失败！"+response.errorMessage);
    }
  }).catch(() => {
    self.$Spin.hide();
    self.$Message.error("导入失败！");
  });
}

export const queryCurrentStation = (self) => {
  self.$apiImpProduct.queryStation().then(response => {
    self.categoryTreeData.splice(0);
    if(response.success) {
      let result = response.result;
      self.station.code = result.code;
      self.station.name = result.name;
    }else{
      self.$Message.error(response.errorMessage);
    }
  });
}

export const bannedProducts = (self, ids) => {
  let params = {ids: ids};
  self.$Spin.show();
  self.$apiImpProduct.bannedImpProducts(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("操作成功！");
      queryProductList(self);
    }else{
      self.$Message.error(response.errorMessage);
    }
  });
}

export const cancelBannedProducts = (self, ids) => {
  let params = {ids: ids};
  self.$Spin.show();
  self.$apiImpProduct.cancelBannedImpProducts(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("操作成功！");
      queryProductList(self);
    }else{
      self.$Message.error(response.errorMessage);
    }
  });
}

export const importImagesToFormalLib = (self) => {
  let params = importData.tParam.importParam;
  self.$Message.info({content: "正在导入图片...", duration: 3});
  self.$Spin.show();
  self.$apiImpProduct.importProductImages(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("导入产品图片完成！");
      // queryProductList(self);
    }else{
      console.error(response);
      self.$Message.error("加入导入队列失败！"+response.errorMessage);
    }
  }).catch(() => {
    self.$Spin.hide();
    console.log("导入产品图片失败！");
  })
}

export const queryImportRecordList = (self) => {
  let params = importData.tParam.importRecordParam;
  self.$Spin.show();
  self.$apiImpProduct.queryImportQueueList(params).then(response => {
    self.$Spin.hide();
    importData.tData.importRecordList.splice(0);
    if(response.success) {
      importData.tData.importRecordList.push(...response.result);
    }else{
      console.error(response);
      self.$Message.error(response.errorMessage);
    }
  }).catch(() => {
    self.$Spin.hide();
    self.$Message.error("查询导入记录失败！");
  })
}

export const reImportProduct = (self, queue, type) => {
  let params = {queueId: queue.queueId, type: type};
  self.$Spin.show();
  self.$apiImpProduct.reImport(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("已重新加入导入队列");
      queryImportRecordList(self, queue.collectionId);
    }else{
      console.error(response);
      self.$Message.error("重新加入导入队列失败！"+response.errorMessage);
    }
  }).catch(() => {
    self.$Spin.hide();
    self.$Message.error("重新加入导入队列失败！");
  });
}

export const deleteImportRecord = (self, queue) => {
  let params = {queueId: queue.queueId};
  self.$Spin.show();
  self.$apiImpProduct.deleteImportRecord(params).then(response => {
    self.$Spin.hide();
    if(response.success) {
      self.$Message.success("删除成功！");
      queryImportRecordList(self, queue.collectionId);
    }else{
      console.error(response);
      self.$Message.error(response.errorMessage);
    }
  }).catch(() => {
    self.$Spin.hide();
    self.$Message.error("删除导入记录失败！");
  })
}

export const resetImportParams = (ids) => {
  importData.tParam.importParam.categories.splice(0);
  importData.tParam.importParam.profitRatio = 100;
  importData.tParam.importParam.ids.splice(0);
  importData.tParam.importParam.ids.push(...ids);
}

export const importProducts = (self, callback) => {
  if(isEmpty(importData.tParam.importParam.profitRatio)) {
    self.$Message.error({content: "利润比不能为空", duration: 2});
    return;
  }
  if(importData.tParam.importParam.profitRatio < 100) {
    self.$Message.error({content: "利润比不能小于100%", duration: 2});
    return;
  }
  if(importData.tParam.importParam.categories.length < 1) {
    self.$Message.error({content: "请选择产品分类", duration: 2});
    return;
  }

  self.$Modal.confirm({
    title: "确认",
    content: "确定导入吗(重复导入会生成一个新的SKU)？",
    okText: "确定",
    cancelText: "取消",
    onOk() {
      let params = importData.tParam.importParam;
      self.$Spin.show();
      self.$apiImpProduct.putInQueue(params).then(response => {
        self.$Spin.hide();
        if(response.success) {
          //关闭弹窗
          importData.tStatus.profitRatioModal.isShow = false;
          //提示导入中
          self.$Message.info({content: "已加入导入队列", duration: 5});
          if(callback) {
            callback(self);
          }
        }else{
          console.error(response.errorMessage);
          self.$Message.error("加入导入队列失败！"+response.errorMessage);
        }
      }).catch(() => {
        self.$Spin.hide();
        self.$Message.error("导入失败！");
      });
    }
  })
}

export const afterImportProducts = (self) => {
  //清空已选列表
  self.resetSelectedProducts();
  //刷新列表
  queryProductList(self);
}
